package wxshop.service.user.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import wxshop.api.user.dao.BalancesDao;
import wxshop.api.user.dao.UserDao;
import wxshop.api.user.exception.DataNotFoundException;
import wxshop.api.user.model.Balances;
import wxshop.api.user.model.OrderRecord;
import wxshop.api.user.model.User;
import wxshop.api.user.service.BalancesService;

import java.util.List;

/**
 * Created by yvdedu.com on 2018/1/25.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
public class BalancesServiceImpl implements BalancesService {
	
	@Autowired
	private BalancesDao balancesDao;
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public int getBalance (int id) {////拿到余额的方法
		return balancesDao.getBalances(id);
	}
	
	//提现  先插入数据  再事务处理 修改余额
	@Override
	public void withdrawals (User user, Balances balances) {//提现  先插入数据  再事务处理 修改余额
		int ye = balancesDao.getBalances(user.getId());
		if (-user.getBalance()>ye) {
			throw new DataNotFoundException("余额不足,请充值");
		}
		balancesDao.insertBalances(balances); //changes + state + tint +user_id
		userDao.updateBalance(user);//修改用户余额数据  id+ balance
	}
	
	//充值  先插入数据  再事务处理 修改余额
	@Override
	public void recharge (User user, Balances balances) {
		
		balancesDao.insertBalances(balances); //changes + state + tint +user_id
		userDao.updateBalance(user);//修改用户余额数据  id+ balance
	}
	
	//查询用户余额明细
	@Override
	public List<Balances> queryBalanceDetailed (Balances input) {
		return balancesDao.queryChangesDetailed(new Balances(input.getUser_id()));
		
//		return null;
	}
	
	@Override
	//订单记录
	public List<OrderRecord> queryOrderRecord (OrderRecord input) {
		return balancesDao.queryOrderRecord(input);
	}
	
}
